//
//  WHHomeTopView.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/2.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHHomeTopView.h"
#import "UIView+YBInputControl.h"

@interface WHHomeTopView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *searchBackView;
@property (nonatomic, strong) UIButton *searchTipsButton;

@property (nonatomic, strong) UIButton *scanButton;

@property (nonatomic, strong) UITextField *orderNumberField;

@property (nonatomic, strong) UIView *countBackView;
@property (nonatomic, strong) UILabel *todayInTipsLabel; // 今日入库
@property (nonatomic, strong) UILabel *todaySignTipsLabel; // 今日签收
@property (nonatomic, strong) UILabel *todayPackageTipsLabel; // 待签收包裹

@property (nonatomic, strong) UILabel *todayInLabel;
@property (nonatomic, strong) UILabel *todaySignLabel;
@property (nonatomic, strong) UILabel *todayPackageLabel;

@property (nonatomic, assign) BOOL isShowSearch;

@end

@implementation WHHomeTopView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _isShowSearch = NO;
        [self setUpTopView];
    }
    return self;
}

- (void)setUpTopView {
    [self addSubview:({
        _scanButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_scanButton setImage:IMAGE_NAMED(@"home_scan_icon") forState:UIControlStateNormal];
        
        _scanButton;
    })];
    
    [_scanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(kStatusBarHeight+kRealwidth(10));
        make.width.height.mas_equalTo(kRealwidth(40));
        make.right.mas_equalTo(self.mas_right).offset(-kRealwidth(15));
    }];
    
    
    [self addSubview:({
        _searchBackView = [[UIView alloc] init];
        _searchBackView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
        ViewRadius(_searchBackView, kRealwidth(34)/2);
        
        _searchBackView;
    })];
    
    [_searchBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(kRealwidth(15));
        make.height.mas_equalTo(kRealwidth(34));
        make.centerY.mas_equalTo(_scanButton.mas_centerY);
        make.width.mas_equalTo(kRealwidth(34));
    }];
    
    [_searchBackView addSubview:({
        _searchTipsButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchTipsButton setImage:IMAGE_NAMED(@"home_search_icon") forState:UIControlStateNormal];
        
        _searchTipsButton;
    })];
    
    [_searchTipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(kRealwidth(20));
        make.centerY.mas_equalTo(_searchBackView.mas_centerY);
        make.left.mas_equalTo(_searchBackView.mas_left).offset(kRealwidth(8));
    }];
    
    [_searchBackView addSubview:({
        _orderNumberField = [[UITextField alloc] init];
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"订单号/手机号/取件号" attributes:@{NSForegroundColorAttributeName:KWhiteColor, NSFontAttributeName:SYSTEMFONT(kRealwidth(17))}];
        _orderNumberField.attributedPlaceholder = attrString;
        _orderNumberField.textColor = KWhiteColor;
        
        _orderNumberField.font = SYSTEMFONT(kRealwidth(17));
        _orderNumberField.keyboardType = UIKeyboardTypeASCIICapable;
        _orderNumberField.returnKeyType = UIReturnKeySearch;
        _orderNumberField.delegate = self;
        
        _orderNumberField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(20).set_regularStr(@"^[a-zA-Z0-9]{0,20}$").set_textChanged(^(id obj) {
            
        });
        
        _orderNumberField;
    })];
    
    [_orderNumberField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_searchTipsButton.mas_right).offset(kRealwidth(15));
        make.height.mas_equalTo(_searchBackView.mas_height);
        make.centerY.mas_equalTo(_searchBackView.mas_centerY);
        make.width.equalTo(@(0));
    }];
    _orderNumberField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _orderNumberField.delegate = self;
    [_scanButton addTarget:self action:@selector(beginScanCicked:) forControlEvents:UIControlEventTouchUpInside];
    [_searchTipsButton addTarget:self action:@selector(showSearch:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:({
        _countBackView = [[UIView alloc] init];
        
        _countBackView;
    })];
    
    [_countBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_searchBackView.mas_bottom).offset(kRealwidth(30));
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.height.mas_equalTo(kRealwidth(55));
    }];
    
    
    [_countBackView addSubview:({
        _todayInLabel = [[UILabel alloc] init];
        _todayInLabel.textColor = KWhiteColor;
        _todayInLabel.textAlignment = NSTextAlignmentCenter;
        _todayInLabel.font = SYSTEMFONT(kRealwidth(27));
        _todayInLabel.adjustsFontSizeToFitWidth = YES;
        _todayInLabel.text = @"0";
        
        _todayInLabel;
    })];
    
    [_todayInLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_countBackView.mas_width).dividedBy(3);
        make.left.mas_equalTo(_countBackView.mas_left);
        make.top.mas_equalTo(_countBackView.mas_top).offset(kRealwidth(5));
        make.height.mas_equalTo(kRealwidth(27));
    }];
    
    [_countBackView addSubview:({
        _todaySignLabel = [[UILabel alloc] init];
        _todaySignLabel.textColor = KWhiteColor;
        _todaySignLabel.textAlignment = NSTextAlignmentCenter;
        _todaySignLabel.font = SYSTEMFONT(kRealwidth(27));
        _todaySignLabel.adjustsFontSizeToFitWidth = YES;
        _todaySignLabel.text = @"0";
        
        _todaySignLabel;
    })];
    
    [_todaySignLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_countBackView.mas_width).dividedBy(3);
        make.left.mas_equalTo(_todayInLabel.mas_right);
        make.top.mas_equalTo(_todayInLabel.mas_top);
        make.height.mas_equalTo(_todayInLabel.mas_height);
    }];
    
    [_countBackView addSubview:({
        _todayPackageLabel = [[UILabel alloc] init];
        _todayPackageLabel.textColor = KWhiteColor;
        _todayPackageLabel.textAlignment = NSTextAlignmentCenter;
        _todayPackageLabel.font = SYSTEMFONT(kRealwidth(27));
        _todayPackageLabel.adjustsFontSizeToFitWidth = YES;
        _todayPackageLabel.text = @"0";

        _todayPackageLabel;
    })];
    
    [_todayPackageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_countBackView.mas_width).dividedBy(3);
        make.left.mas_equalTo(_todaySignLabel.mas_right);
        make.top.mas_equalTo(_todayInLabel.mas_top);
        make.height.mas_equalTo(_todayInLabel.mas_height);
    }];
    
    [_countBackView addSubview:({
        _todayInTipsLabel = [[UILabel alloc] init];
        _todayInTipsLabel.textColor = KWhiteColor;
        _todayInTipsLabel.textAlignment = NSTextAlignmentCenter;
        _todayInTipsLabel.font = SYSTEMFONT(kRealwidth(15));
        _todayInTipsLabel.text = @"今日入库";
        
        _todayInTipsLabel;
    })];
    
    [_todayInTipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_countBackView.mas_width).dividedBy(3);
        make.centerX.mas_equalTo(_todayInLabel.mas_centerX);
        make.top.mas_equalTo(_todayInLabel.mas_bottom).offset(kRealwidth(15));
        make.height.mas_equalTo(kRealwidth(15));
    }];
    
    [_countBackView addSubview:({
        _todaySignTipsLabel = [[UILabel alloc] init];
        _todaySignTipsLabel.textColor = KWhiteColor;
        _todaySignTipsLabel.textAlignment = NSTextAlignmentCenter;
        _todaySignTipsLabel.font = SYSTEMFONT(kRealwidth(15));
        _todaySignTipsLabel.text = @"今日签收";
        
        _todaySignTipsLabel;
    })];
    
    [_todaySignTipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_countBackView.mas_width).dividedBy(3);
        make.centerX.mas_equalTo(_todaySignLabel.mas_centerX);
        make.top.mas_equalTo(_todayInTipsLabel.mas_top);
        make.height.mas_equalTo(kRealwidth(15));
    }];
    
    [_countBackView addSubview:({
        _todayPackageTipsLabel = [[UILabel alloc] init];
        _todayPackageTipsLabel.textColor = KWhiteColor;
        _todayPackageTipsLabel.textAlignment = NSTextAlignmentCenter;
        _todayPackageTipsLabel.font = SYSTEMFONT(kRealwidth(15));
        _todayPackageTipsLabel.text = @"待签收包裹";

        _todayPackageTipsLabel;
    })];
    
    [_todayPackageTipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_countBackView.mas_width).dividedBy(3);
        make.centerX.mas_equalTo(_todayPackageLabel.mas_centerX);
        make.top.mas_equalTo(_todayInTipsLabel.mas_top);
        make.height.mas_equalTo(kRealwidth(15));
    }];
    
}

- (void)showSearch:(UIButton *)button {
    [self showSearch];
    [_orderNumberField becomeFirstResponder];
}


- (void)showSearch {
    if (!_isShowSearch) {
        _isShowSearch = YES;
        [UIView animateWithDuration:.3f animations:^{
            [_searchBackView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.mas_left).offset(kRealwidth(15));
                make.right.mas_equalTo(_scanButton.mas_left).offset(-kRealwidth(15));
                make.height.mas_equalTo(kRealwidth(34));
                make.centerY.mas_equalTo(_scanButton.mas_centerY);
            }];
            
            [_orderNumberField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(_searchTipsButton.mas_right).offset(kRealwidth(15));
                make.height.mas_equalTo(_searchBackView.mas_height);
                make.centerY.mas_equalTo(_searchBackView.mas_centerY);
                make.right.mas_equalTo(_searchBackView.mas_right).offset(-kRealwidth(15));
            }];
            
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
    } else {
        
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self endEditing:YES];
    if (self.beginSearch) {
        self.beginSearch(_orderNumberField.text);
    }
    
    return YES;
}

- (void)beginScanCicked:(UIButton *)button {
    if (self.beginScan) {
        self.beginScan();
    }
}

- (void)setSearchText:(NSString *)searchText {
    _searchText = searchText;
    _orderNumberField.text = searchText;
}

- (void)setIntoStatus:(NSString *)intoStatus {
    _intoStatus = intoStatus;
    
    _todayInLabel.text = intoStatus;
}

- (void)setOutStatus:(NSString *)outStatus {
    _outStatus = outStatus;
    _todaySignLabel.text = outStatus;
}

- (void)setWaitNumber:(NSString *)waitNumber {
    _waitNumber = waitNumber;
    _todayPackageLabel.text = waitNumber;
}

@end
